iT邦幫忙

2022 iThome 鐵人賽

DAY 29
0
Modern Web

clojure 刷刷鍋系列 第 29

Clojure 肉片 -第 29 塊

  • 分享至 

  • xImage
  •  

【今日湯底】

Take 2 strings s1 and s2 including only letters from a to z. Return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2.

Examples:
a = "xyaabbbccccdefww"
b = "xxxxyyyyabklmopq"
longest(a, b) -> "abcdefklmopqwxy"

a = "abcdefghijklmnopqrstuvwxyz"
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"

(必須通過以下測試)

(ns longest.core-test
  (:require [clojure.test :refer :all]
            [longest.core :refer :all]))
(require '[clojure.string :as str])

(defn test-assert [act exp]
  (is (= act exp)))

(deftest a-test1
  (testing "longest"
    (test-assert(longest "aretheyhere", "yestheyarehere"), "aehrsty")
    (test-assert(longest "loopingisfunbutdangerous", "lessdangerousthancoding"), "abcdefghilnoprstu")
    (test-assert(longest "inmanylanguages", "theresapairoffunctions"), "acefghilmnoprstuy")  
))

【我的答案】

(ns longest.core)
(require '[clojure.string :as str])

(defn longest [s1 s2]
  (str/join (sort (distinct (str/join s1 s2))))
  )

【其他人的答案】

(ns longest.core)

(defn longest [& ss]
  (->> ss (apply concat) (apply sorted-set) (apply str)))

上一篇
Clojure 肉片 -第 28 塊
下一篇
Clojure 肉片 -第 30 塊
系列文
clojure 刷刷鍋30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言